package comWorld.blog.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gjs.tools.utils.Utils;

import comWorld.blog.BlogInfo;
import comWorld.blog.BlogInfoManage;
import comWorld.blog.BlogInfos;
import comWorld.blogCatalog.BlogCatalogs;
import comWorld.social.SocialInfo;
import comWorld.social.SocialManage;
import comWorld.social.VisitorCount;
import comWorld.user.UserInfo;
import comWorld.user.UserStatus;

/**
 * Servlet implementation class AllBlogPageInit
 */
@WebServlet("/blog")
public class AllBlogPageInit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AllBlogPageInit() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserStatus visitorStatus = (UserStatus) request.getSession().getAttribute("visitorStatus");
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
		
		VisitorCount visitorCount = VisitorCount.getLastVisitorCount(userInfo.getId());
		visitorCount.add();
		request.setAttribute("visitorCount", visitorCount);
		
		int privacy = 0;
		if (userInfo.getId() != visitorStatus.getId())
			privacy = 1;
		
		BlogCatalogs blogCatalogs = new BlogCatalogs(userInfo.getId());
		blogCatalogs.statisticsBlogCount(userInfo.getId(), privacy);
		request.setAttribute("blogCatalogs", blogCatalogs);
		request.setAttribute("hotBlogs", BlogInfoManage.getHotBlogs(userInfo.getId()));
		SocialInfo socialInfo = (SocialInfo) request.getAttribute("socialInfo");
		
		String catalog = request.getParameter("catalog");
		int currentCatalog = (catalog == null) ? 0 : Integer.valueOf(catalog);
		String sort = request.getParameter("sort");
		sort = ((sort == null || sort.length() == 0) ? "time" : sort);
		
		request.setAttribute("currentCatalog", currentCatalog);
		request.setAttribute("sort", sort);
		
		BlogInfos blogInfos = BlogInfoManage.getBlogInfosByCatalogIdAndSort(userInfo.getId(), currentCatalog, sort, privacy);
		List<BlogInfo> blogList = blogInfos.getBlogs();
		
		
		int[] be = Utils.paging(request, 10, blogList.size());
		
		request.setAttribute("blogList", blogList.subList(be[0], be[1]));
		
		int userId = (int) request.getAttribute("userId");
		String forward = "/blog/allBlogs.jsp";
		if (!visitorStatus.isLogin() || visitorStatus.getId() != userId) {
			request.setAttribute("followed", SocialManage.judgeUserHasFollowed(visitorStatus, socialInfo, userId));
			forward = "/blog/otherAllBlogs.jsp";
		}
		request.getRequestDispatcher(forward).forward(request, response);
	}

}
